package inits

import (
	"day5/user-sage/auth-service/basic/handler/model"
	"day5/user-sage/auth-service/config"
	"fmt"
)

var err error

func Mysql() {
	MysqlData := config.Config.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", MysqlData.User, MysqlData.Password, MysqlData.Host, MysqlData.Port, MysqlData.Database)
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("数据库连接失败")
	} else {
		fmt.Println("数据库连接成功")
	}
	err = config.DB.AutoMigrate(&model.User{})
	if err != nil {
		panic("数据表迁移失败")
		return
	} else {
		fmt.Println("数据表迁移成功")
	}
}
